home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac: Not for Sale / Another.not.for.sale (Australia).iso / fade into you / getting there / TCP⁄PPP⁄SLIP / TCP⁄IP Scripting Addition 1.1.2 / Examples 1.2 / Text Versions / Baby Telnet (text) next >
Text File  |  1994-09-18  |  5KB  |  166 lines

  1. (*
  2. Baby Telnet v1.1
  3. Copyright (c) 1994 by Atul Butte. All Rights Reserved.
  4.  
  5. Needs AppleScript 1.1, MacTCP 2.0.4, Scriptable Text Editor and TCP/IP Scripting Addition 1.1
  6. *)
  7.  
  8. global respondedToOurEcho
  9. global respondedToOurSGA
  10.  
  11. global terminalType
  12.  
  13. on in_english(ttt)
  14.     set acc to {}
  15.     repeat with c in (characters of ttt)
  16.         set d to ASCII number of (contents of c)
  17.         if d = 255 then
  18.             set d to "iac"
  19.         else if d = 253 then
  20.             set d to "do"
  21.         else if d = 254 then
  22.             set d to "dont"
  23.         else if d = 251 then
  24.             set d to "will"
  25.         else if d = 252 then
  26.             set d to "wont"
  27.         else if d = 250 then
  28.             set d to "sb"
  29.         else if d = 240 then
  30.             set d to "se"
  31.         end if
  32.         set acc to acc & d
  33.     end repeat
  34.     return {acc, ttt}
  35. end in_english
  36.  
  37. on firstConnect()
  38.     set respondedToOurEcho to false
  39.     set respondedToOurSGA to false
  40.     return (ASCII character (255)) & (ASCII character (253)) & (ASCII character (1)) & (ASCII character (255)) & (ASCII character (253)) & (ASCII character (3)) & (ASCII character (255)) & (ASCII character (251)) & (ASCII character (24))
  41. end firstConnect
  42.  
  43. on parseAndRespond(incoming)
  44.     set toReturn to ""
  45.     set oldDelimiters to AppleScript's text item delimiters
  46.     set AppleScript's text item delimiters to {ASCII character 255}
  47.     set incoming to "**" & incoming
  48.     set d to text items of incoming
  49.     set AppleScript's text item delimiters to oldDelimiters
  50.     if length of d = 1 then
  51.         return ""
  52.     end if
  53.     repeat with itemcode in (items 2 through end of d)
  54.         set theitem to contents of itemcode
  55.         set code to ASCII number (character 1 of theitem)
  56.         if (length of theitem > 1) then
  57.             set optionchar to character 2 of theitem
  58.             set option to ASCII number (optionchar)
  59.             --log "code: " & code & " option: " & option
  60.         else
  61.             set optionchar to ""
  62.             --log "code: " & code
  63.         end if
  64.         if code = 250 and option = 24 then
  65.             --log "sending terminal vt100"
  66.             set toReturn to toReturn & (ASCII character 255) & (ASCII character 250) & (ASCII character 24) & ¬
  67.                 (ASCII character 0) & terminalType & (ASCII character 255) & (ASCII character 240)
  68.         end if
  69.         if code = 253 then
  70.             if option = 24 then
  71.             else -- server says iac do, we say iac wont
  72.                 set toReturn to toReturn & (ASCII character 255) & (ASCII character 252) & optionchar
  73.             end if
  74.         end if
  75.         if code = 251 then -- server says iac will
  76.             if (option = 1 or option = 3) then
  77.                 if option = 1 and not respondedToOurEcho then
  78.                     set respondedToOurEcho to true
  79.                 else if option = 3 and not respondedToOurSGA then
  80.                     set respondedToOurSGA to true
  81.                 else -- server says iac will, we say iac do
  82.                     set toReturn to toReturn & (ASCII character 255) & (ASCII character 253) & optionchar
  83.                 end if
  84.             else
  85.                 set toReturn to toReturn & (ASCII character 255) & (ASCII character 254) & optionchar
  86.             end if
  87.         end if
  88.         if code = 254 then -- server says iac dont, we say iac wont
  89.             set toReturn to toReturn & (ASCII character 255) & (ASCII character 252) & optionchar
  90.         end if
  91.         if code = 252 then -- server says iac wont, we say iac dont
  92.             set toReturn to toReturn & (ASCII character 255) & (ASCII character 254) & optionchar
  93.         end if
  94.     end repeat
  95.     return toReturn
  96. end parseAndRespond
  97.  
  98.  
  99.  
  100. set theHost to text returned of (display dialog "Login to where?" default answer "")
  101. set terminalType to text returned of (display dialog "What kind of terminal should I say you have (all caps, please)?" default answer "VT100")
  102. set sss to (tcp connect to host theHost port 23)
  103. tcp write stream sss data (firstConnect())
  104.  
  105. try
  106.     tell application "Scriptable Text Editor"
  107.         activate
  108.         make new document at beginning
  109.         set selection to "(Once you see the login prompt, start typing at the > prompt; " & return & "type 'stop' to stop the script and disconnect)" & return
  110.         set style of paragraphs -3 through -2 of window 1 to {class:text style info, on styles:{bold}, off styles:{italic, underline, outline, shadow}}
  111.         set selection to ">"
  112.         set continueloop to true
  113.         repeat while continueloop
  114.             set t to last paragraph of window 1
  115.             if t = "" then
  116.                 set oneBefore to (paragraph -2 of window 1) as string
  117.                 if oneBefore = ">stop" then
  118.                     exit repeat
  119.                 end if
  120.                 set selection to ">"
  121.                 if oneBefore = ">" then
  122.                     tcp write stream sss data return
  123.                 else
  124.                     set toSend to ((characters 2 through end of oneBefore) as string) & return
  125.                     tcp write stream sss data toSend
  126.                 end if
  127.             end if
  128.             set tcpstat to (tcp status stream sss)
  129.             if (connection status of tcpstat ≠ Connected) then
  130.                 exit repeat
  131.             end if
  132.             set didWePrintSomething to false
  133.             repeat while (bytes waiting of tcpstat > 0)
  134.                 set incoming to (tcp read stream sss as text)
  135.                 if (length of incoming > 0) then
  136.                     if (number of characters of window 1 > 20000) then
  137.                         select (text from character 1 to character 20000)
  138.                         delete selection
  139.                     end if
  140.                     if not didWePrintSomething then
  141.                         set selection to return & "["
  142.                         set didWePrintSomething to true
  143.                     end if
  144.                     set selection to incoming
  145.                     set toSend to (my parseAndRespond(incoming))
  146.                     if (length of toSend > 0) then
  147.                         tcp write stream sss data toSend
  148.                     end if
  149.                 end if
  150.                 set tcpstat to (tcp status stream sss)
  151.                 if (connection status of tcpstat ≠ Connected) then
  152.                     exit repeat
  153.                 end if
  154.             end repeat
  155.             if didWePrintSomething then
  156.                 set selection to "]" & return
  157.                 set selection to ">"
  158.             end if
  159.         end repeat
  160.         tcp close stream sss
  161.     end tell
  162. on error msg number num
  163.     tcp close stream sss
  164.     error msg number num
  165. end try
  166.